package cn.itcast.service.impl;

import cn.itcast.constant.RedisConstant;
import cn.itcast.pojo.Member;
import cn.itcast.service.LoginServcie;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.Duration;
import java.util.List;

/**
 * @author coolbook
 * @ClassName LoginServiceImpl
 * @Description TODO
 * @Date 21:03 2021/11/18
 * @Version 1.0
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginServcie {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MemberService memberService;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @Override
    public String sendLoginCode(String phone) {
        //先去查询该手机用户是否是禁用状态
       // List<Member> memberList = this.memberService.findMemberByPhone(phone);
//        Boolean flag = false;
//        for (Member member : memberList) {
//            if (member.getStatus()==1){
//                flag = true;
//                break;
//            }
//        }
//        if (flag){
//            return "1";
//        }
        //先去redis中查询有没有验证码
        Boolean result = this.redisTemplate.hasKey(RedisConstant.SENDTYPE_LOGIN + phone);
        if (result){
            return null;
        }
        try {
//            int code = RandomUtils.nextInt(100000, 999999);
//            String strCode = String.valueOf(code);
            //   SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phone,"123456");
            //把验证码保存到redis中去 失效时间5分钟
            this.redisTemplate.opsForValue().set(RedisConstant.SENDTYPE_LOGIN+phone,"123465", Duration.ofSeconds(300));
            return "001";
        } catch (Exception e) {
            log.error("发送短信验证码失败 phone:{}",phone,e);
        }

        return null;
    }


    /**
     * 校验登录验证码
     * @param phone
     * @param code
     * @return
     */
    @Override
    public Boolean login(String phone,String code){
        String data = this.redisTemplate.opsForValue().get(RedisConstant.SENDTYPE_LOGIN+ phone);
        if (StringUtils.equals(code,data)){
            //删除redis中的数据
            this.redisTemplate.delete(RedisConstant.SENDTYPE_LOGIN+phone);
            //把对象标识放到redis中做
            //把手机号存到redis中
            this.redisTemplate.opsForValue().set(phone,"1",Duration.ofDays(15));
            return true;
        }
        return false;
    }

    /**
     * 预约电话号码
     * @param phone
     * @return
     */
    @Override
    public String sendOrderCode(String phone) {

        //先去redis中查询有没有电话号码
        Boolean result = this.redisTemplate.hasKey(RedisConstant.SENDTYPE_ORDER + phone);
        if (result){
            return null;
        }
        try {
            //int code = RandomUtils.nextInt(100000, 999999);
//            String strCode = String.valueOf(code);
            //   SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phone,strCode);
            //把验证码保存redis中去

            this.redisTemplate.opsForValue().set(RedisConstant.SENDTYPE_ORDER+phone,"123456");
            return "xixi";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 校验预约验证码
     * @param phone
     * @param validateCode
     * @return
     */
    @Override
    public Boolean checkOrderCode(String phone, String validateCode) {
        String data = this.redisTemplate.opsForValue().get(RedisConstant.SENDTYPE_ORDER + phone);
        if(StringUtils.equals(data,validateCode)){
            //删除redis中的数据
            this.redisTemplate.delete(RedisConstant.SENDTYPE_ORDER+phone);
            return true;
        }

        return false;
    }
}
